package dao;

import entity.user;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import tools.druid;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author malguy-wang sir
 * @create ---
 */
public class userDaoImp implements userDao {
    private druid druid = new druid();
    QueryRunner qr = druid.getQr();
    Connection conn;

    {
        try {
            conn = druid.druid();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<user> findAll() {
        String sql = "select * from user";
        BeanListHandler<user> handler = new BeanListHandler<>(user.class);
        List<user> list = null;
        try {
            list = qr.query(conn, sql, handler);
            return list;
        } catch (SQLException throwables) {
        } finally {
            druid.close(conn, null);
        }
        return null;
    }

    @Override
    public boolean findByName(String username) {
        try {
            String sql = "select * from user where username = ?";
            BeanHandler<user> handler = new BeanHandler<>(user.class);
            user user = qr.query(conn, sql, handler, username);
            return true;
        } catch (Exception e) {
        } finally {
            druid.close(conn, null);
        }
        return false;
    }
}

